//   Copyright 2012,2013 Vaughn Vernon
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

package com.saasovation.collaboration.port.adapter.persistence;

import com.saasovation.common.event.sourcing.EventStore;
import org.springframework.jdbc.core.JdbcTemplate;

public class StorageCleaner {

    private static final String[] tablesToClean = {
            "tbl_dispatcher_last_event",
            "tbl_es_event_store",
            "tbl_vw_calendar",
            "tbl_vw_calendar_sharer",
            "tbl_vw_calendar_entry",
            "tbl_vw_calendar_entry_invitee",
            "tbl_vw_forum",
            "tbl_vw_discussion",
            "tbl_vw_post"
    };

    private final JdbcTemplate jdbcTemplate;
    private final EventStore eventStore;

    public StorageCleaner(JdbcTemplate jdbcTemplate,
                          EventStore eventStore) {
        super();
        this.jdbcTemplate = jdbcTemplate;
        this.eventStore = eventStore;
    }

    public void clean() {
        this.eventStore.purge();
        for (String tableName : tablesToClean) {
            this.jdbcTemplate.execute("delete from " + tableName);
        }
    }
}
